<!--
 * @Description: 
 * @Author: jun.wu
 * @Date: 2022-10-18 15:10:12
 * @LastEditors: jun.wu
 * @LastEditTime: 2022-10-24 13:35:20
-->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 放置一个el-tree -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点循环多少次 -->
          <!-- 作用域插槽 data是每个节点的数据对象 -->
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <!-- 放置新增弹窗组件 -->
      <addDept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :tree-node="node"
        @addDepts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import addDept from "./components/add-dept.vue";

import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
export default {
  components: {
    TreeTools,
    addDept,
  },
  data() {
    return {
      company: {}, //头部的数据结构
      departs: [],
      defaultProps: {
        label: "name", // 表示从这个属性显示内容
      },
      showDialog: false,
      node: null,
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments();
      this.company = { id: "", name: result.companyName, manager: "负责人" };
      this.departs = tranListToTreeData(result.depts, "");
    },
    //新增
    addDepts(node) {
      this.showDialog = true; //显示弹窗
      this.node = node;
    },
    // 编辑
    editDepts(node) {
      this.showDialog = true;
      this.node = node;
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
